package com.zhongkai.app;

import com.zhongkai.Bean.BigUnitBean;
import com.zhongkai.Bean.ErrorCodeBean;
import com.zhongkai.MessageTool;
import com.zhongkai.StringStatic;
import com.zhongkai.app.service.BigDataUnitServcie;
import com.zhongkai.jedis.JedisClient;
import org.apache.log4j.Logger;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.common.message.MessageExt;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.StringTokenizer;

/**
 * rocketMQ
 */
public class ListenerConcurrentlySky implements MessageListenerConcurrently {

    private static Logger logger = Logger.getLogger(ListenerConcurrentlySky.class);


    @Autowired
    private BigDataUnitServcie bigDataUnitServcie;

    @Autowired
    private JedisClient jedisClient;


    //private static String times;
    // private static long time1;

    final Long diff = 100L;

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        try {

            for (MessageExt msg : list) {
              //  Message msg = list.get(0);



                long queueOffset = msg.getQueueOffset();

                String property = msg.getProperty(MessageConst.PROPERTY_MAX_OFFSET);

                long pan = Long.parseLong(property) - queueOffset;


                if (pan > diff){
                    System.out.println("消息堆积");
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }



                String json = new String(msg.getBody(), "utf-8");

                String keys = msg.getTags();

                ObjectMapper mapper = new ObjectMapper(); //解析json

           //     logger.info(msg.getTopic() + "-" + msg.getTags() + ":"+keys+"-------"+ json);



                //  System.out.println(json);
                switch (keys) {


                    case MessageTool.UNIT_BIG:


                        BigUnitBean bigUnitBean = mapper.readValue(json, BigUnitBean.class);  //当前到来的最新数据


                        String big_unit_uuid = bigUnitBean.getUnit_uuid(); //取出设备uuid


                        if (!"0".equals(bigUnitBean.getWarning())) { //设备报警 直接存储

/*                        bigDataUnitServcie.updateUnit(
                                big_unit_uuid, //设备id
                                bigUnitBean.getState(), //状态
                                bigUnitBean.getPower_all(), //总电量
                                bigUnitBean.getWarning(), //是否报警
                                bigUnitBean.getUser_t()//设备当前温度

                        );*/
                            jedisClient.set("IS_ERR".concat(big_unit_uuid),"1");

                            bigDataUnitServcie.addErrorData(
                                    bigUnitBean.getState(),
                                    bigUnitBean.getSignal(),
                                    bigUnitBean.getPower(),
                                    bigUnitBean.getForce(),
                                    bigUnitBean.getVoltage_currente(),
                                    bigUnitBean.getPower_all(),
                                    bigUnitBean.getUser_t(),
                                    bigUnitBean.getTime(),
                                    bigUnitBean.getUser_water_r(),
                                    bigUnitBean.getUser_water_s(),
                                    bigUnitBean.getUser_stressr_r(),
                                    bigUnitBean.getUser_stressr_s(),
                                    bigUnitBean.getGround_water_r(),
                                    bigUnitBean.getGround_water_s(),
                                    bigUnitBean.getGround_stressr_r(),
                                    bigUnitBean.getGround_stressr_s(),
                                    bigUnitBean.getUnit_in(),
                                    bigUnitBean.getUnit_out(),
                                    bigUnitBean.getEnvironment(),
                                    bigUnitBean.getSupply_pressure(),
                                    big_unit_uuid,
                                    bigUnitBean.getWarning(),
                                    bigUnitBean.getWarning_code(),
                                    ErrorCodeBean.err_code(bigUnitBean.getWarning_code()),//此处需要根据错误代码 存入对应错误原因

                                    bigUnitBean.getSys_stop(),
                                    bigUnitBean.getCompressor(),
                                    bigUnitBean.getGroundpump(),
                                    bigUnitBean.getSysopen_num(),
                                    bigUnitBean.getCompreopen_num(),
                                    bigUnitBean.getLocking()
                            );


                            jedisClient.setkeyexp(StringStatic.REDIS_UNIT + big_unit_uuid, json, 300);
                            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

                        } else {
/*                        bigDataUnitServcie.updateUnit(
                                big_unit_uuid, //设备id
                                bigUnitBean.getState(), //状态
                                bigUnitBean.getPower_all(), //总电量
                                bigUnitBean.getWarning(), //是否报警
                                bigUnitBean.getUser_t()//设备当前温度
                                //  bigUnitBean.getIp() //设备ip
                        );*/

                            //存入正常数据
                            bigDataUnitServcie.addUnitData(
                                    bigUnitBean.getState(),
                                    bigUnitBean.getSignal(),
                                    bigUnitBean.getPower(),
                                    bigUnitBean.getForce(),
                                    bigUnitBean.getVoltage_currente(),
                                    bigUnitBean.getPower_all(),
                                    bigUnitBean.getUser_t(),
                                    bigUnitBean.getTime(),
                                    bigUnitBean.getUser_water_r(),
                                    bigUnitBean.getUser_water_s(),
                                    bigUnitBean.getUser_stressr_r(),
                                    bigUnitBean.getUser_stressr_s(),
                                    bigUnitBean.getGround_water_r(),
                                    bigUnitBean.getGround_water_s(),
                                    bigUnitBean.getGround_stressr_r(),
                                    bigUnitBean.getGround_stressr_s(),
                                    bigUnitBean.getUnit_in(),
                                    bigUnitBean.getUnit_out(),
                                    bigUnitBean.getEnvironment(),
                                    bigUnitBean.getSupply_pressure(),
                                    big_unit_uuid,
                                    bigUnitBean.getWarning(),
                                    bigUnitBean.getWarning_code(),

                                    bigUnitBean.getSys_stop(),
                                    bigUnitBean.getCompressor(),
                                    bigUnitBean.getGroundpump(),
                                    bigUnitBean.getSysopen_num(),
                                    bigUnitBean.getCompreopen_num(),
                                    bigUnitBean.getLocking()


                            );



                            jedisClient.setkeyexp(StringStatic.REDIS_UNIT + big_unit_uuid, json, 300);
                            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

                        }


                    case "1001":
                        StringTokenizer token = new StringTokenizer(json, ",");

                        String[] data_arr = new String[token.countTokens()];

                        int count = 0;

                        int type;

                        while (token.hasMoreTokens()) {


                            data_arr[count] = token.nextToken();
                            count++;
                        }

                        //System.out.println(data_arr[21]);
                        if (!"0".equals(data_arr[21])) { //设备报警 直接存储
                            //更新设备数据
/*                        bigDataUnitServcie.updateUnit(
                                data_arr[20], //设备id
                                data_arr[1], //状态
                                data_arr[6], //总电量
                                data_arr[21], //是否报警
                                data_arr[7]//设备当前温度
                               );*/
                          //  System.out.println("报警");



                            jedisClient.set("IS_ERR".concat(data_arr[20]),"1");
                            bigDataUnitServcie.addErrorData2(
                                    data_arr[1],
                                    data_arr[2],
                                    data_arr[3],
                                    data_arr[4],
                                    data_arr[5],
                                    data_arr[6],
                                    data_arr[7],
                                    Long.parseLong(data_arr[29]),
                                    data_arr[8],
                                    data_arr[9],
                                    data_arr[10],
                                    data_arr[11],
                                    data_arr[12],
                                    data_arr[13],
                                    data_arr[14],
                                    data_arr[15],
                                    data_arr[16],
                                    data_arr[17],
                                    data_arr[18],
                                    data_arr[19],
                                    data_arr[20],
                                    data_arr[21],
                                    data_arr[22],
                                    ErrorCodeBean.err_code2(data_arr[22] ),//此处需要根据错误代码 存入对应错误原因,
                                    data_arr[23],
                                    data_arr[24],
                                    data_arr[25],
                                    data_arr[26],
                                    data_arr[27],
                                    data_arr[28]
                            );


                            BigUnitBean bigUnitBean2 = new BigUnitBean();
                            bigUnitBean2.setState( data_arr[1]);
                            bigUnitBean2.setSignal( data_arr[2]);
                            bigUnitBean2.setPower( data_arr[3]);
                            bigUnitBean2.setForce( data_arr[4]);
                            bigUnitBean2.setVoltage_currente( data_arr[5]);
                            bigUnitBean2.setPower_all( data_arr[6]);
                            bigUnitBean2.setUser_t( data_arr[7]);
                            bigUnitBean2.setUser_water_r( data_arr[8]);
                            bigUnitBean2.setUser_water_s( data_arr[9]);
                            bigUnitBean2.setUser_stressr_r( data_arr[10]);
                            bigUnitBean2.setUser_stressr_s( data_arr[11]);
                            bigUnitBean2.setUser_water_r( data_arr[12]);
                            bigUnitBean2.setUser_water_s( data_arr[13]);
                            bigUnitBean2.setGround_stressr_r( data_arr[14]);
                            bigUnitBean2.setGround_stressr_s( data_arr[15]);
                            bigUnitBean2.setUnit_in( data_arr[16]);
                            bigUnitBean2.setUnit_out( data_arr[17]);
                            bigUnitBean2.setEnvironment( data_arr[18]);
                            bigUnitBean2.setSupply_pressure( data_arr[19]);
                            bigUnitBean2.setUnit_uuid( data_arr[20]);
                            bigUnitBean2.setWarning( data_arr[21]);
                            bigUnitBean2.setWarning_code( data_arr[22]);
                            bigUnitBean2.setSys_stop( data_arr[23]);
                            bigUnitBean2.setCompressor( data_arr[24]);
                            bigUnitBean2.setSysopen_num( data_arr[25]);
                            bigUnitBean2.setCompreopen_num( data_arr[26]);
                            bigUnitBean2.setLocking( data_arr[27]);




                            jedisClient.setkeyexp(StringStatic.REDIS_UNIT +  data_arr[20], mapper.writeValueAsString(bigUnitBean2), 300);

                            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

                        }else {
/*                        bigDataUnitServcie.updateUnit(
                                data_arr[20], //设备id
                                data_arr[1], //状态
                                data_arr[6], //总电量
                                data_arr[21], //是否报警
                                data_arr[7]//设备当前温度
                        );  //设备ip*/


                            //存入正常数据
                            bigDataUnitServcie.addUnitData(
                                    data_arr[1],
                                    data_arr[2],
                                    data_arr[3],
                                    data_arr[4],
                                    data_arr[5],
                                    data_arr[6],
                                    data_arr[7],
                                    Long.parseLong(data_arr[29]),
                                    data_arr[8],
                                    data_arr[9],
                                    data_arr[10],
                                    data_arr[11],
                                    data_arr[12],
                                    data_arr[13],
                                    data_arr[14],
                                    data_arr[15],
                                    data_arr[16],
                                    data_arr[17],
                                    data_arr[18],
                                    data_arr[19],
                                    data_arr[20],
                                    data_arr[21],
                                    data_arr[22],
                                    data_arr[23],
                                    data_arr[24],
                                    data_arr[25],
                                    data_arr[26],
                                    data_arr[27],
                                    data_arr[28]
                            );
                            //  StringStatic.REDIS_UNIT

  /*                      jedisClient.set(StringStatic.REDIS_UNIT + big_unit_uuid, json); //缓存300秒数据
                        jedisClient.expire(StringStatic.REDIS_UNIT + big_unit_uuid, 300);//重新设置过期时间*/

                            //    jedisClient.setkeyexp(StringStatic.REDIS_UNIT + data_arr[20], json, 300);


                            BigUnitBean bigUnitBean2 = new BigUnitBean();
                            bigUnitBean2.setState( data_arr[1]);
                            bigUnitBean2.setSignal( data_arr[2]);
                            bigUnitBean2.setPower( data_arr[3]);
                            bigUnitBean2.setForce( data_arr[4]);
                            bigUnitBean2.setVoltage_currente( data_arr[5]);
                            bigUnitBean2.setPower_all( data_arr[6]);
                            bigUnitBean2.setUser_t( data_arr[7]);
                            bigUnitBean2.setUser_water_r( data_arr[8]);
                            bigUnitBean2.setUser_water_s( data_arr[9]);
                            bigUnitBean2.setUser_stressr_r( data_arr[10]);
                            bigUnitBean2.setUser_stressr_s( data_arr[11]);
                            bigUnitBean2.setUser_water_r( data_arr[12]);
                            bigUnitBean2.setUser_water_s( data_arr[13]);
                            bigUnitBean2.setGround_stressr_r( data_arr[14]);
                            bigUnitBean2.setGround_stressr_s( data_arr[15]);
                            bigUnitBean2.setUnit_in( data_arr[16]);
                            bigUnitBean2.setUnit_out( data_arr[17]);
                            bigUnitBean2.setEnvironment( data_arr[18]);
                            bigUnitBean2.setSupply_pressure( data_arr[19]);
                            bigUnitBean2.setUnit_uuid( data_arr[20]);
                            bigUnitBean2.setWarning( data_arr[21]);
                            bigUnitBean2.setWarning_code( data_arr[22]);
                            bigUnitBean2.setSys_stop( data_arr[23]);
                            bigUnitBean2.setCompressor( data_arr[24]);
                            bigUnitBean2.setSysopen_num( data_arr[25]);
                            bigUnitBean2.setCompreopen_num( data_arr[26]);
                            bigUnitBean2.setLocking( data_arr[27]);




                            jedisClient.setkeyexp(StringStatic.REDIS_UNIT +  data_arr[20], mapper.writeValueAsString(bigUnitBean2), 300);
                            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                        }




                }
            }





        } catch (Exception e) {
            e.printStackTrace();
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }

        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}

